﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace Team6_Algorithms.Filters
{
    /// <summary>
    /// This class contains functions to paint a direction map
    /// </summary>
   public class ColorDirections
    {
       static Color red = Color.Red;
       static Color blue = Color.Blue;
       static Color black = Color.Black;
       static Color yellow = Color.Yellow;
       static Color green = Color.Green;

       /// <summary>
       /// Made by Dylan Snel
       /// Method to color image according to an DirectionMap
       /// </summary>
       /// <param name="B">the source bitmap</param>
       /// <param name="Directions">the instance of the directionmap to use.</param>
       /// <returns></returns>
        public static bool applyFilter(Bitmap B, int[,] Directions)
        {

            

            Bitmap bSrc = (Bitmap)B.Clone();

            BitmapData BmData = B.LockBits(new Rectangle(0, 0, B.Width, B.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            BitmapData BmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            
            int stride = BmData.Stride;

            System.IntPtr Scan0 = BmData.Scan0;
            System.IntPtr SrcScan0 = BmSrc.Scan0;

            unsafe
            {
                
                byte* p = (byte*)(void*)Scan0;
                byte* pSrc = (byte*)(void*)SrcScan0;

                

                int nOffset = B.Width % 4;
                int nWidth = B.Width;
                int nHeight = B.Height;

                for (int y = 0; y < nHeight; ++y)
                {
                    for (int x = 0; x < nWidth; ++x)
                    {
                        Color useColor;
                        switch (Directions[x, y])
                        {
                            case 1:
                                useColor = red;
                                break;

                            case 2:
                                useColor = blue;
                                break;
                            case 3:
                                useColor = yellow;
                                break;
                            case 4:
                                useColor = green;
                                break;

                            default:
                                useColor = black;
                                break;
                                
                        



                        }
                       // B.SetPixel(x, y, useColor);
                        
                        p[0] = useColor.B;
                        p[1] = useColor.G;
                        p[2] = useColor.R;
                         

                        p += 3;
                        // add 3 pixels to the address? possibly
                        // at least, this is moving through the pic
                        // or maybe 3 bytes
                        pSrc += 3;
                    }
                    // but then here we add the padding... at the end of every row
                    
                    p += nOffset;
                    pSrc += nOffset;
                     
                }
            }
            
            B.UnlockBits(BmData);
            bSrc.UnlockBits(BmSrc);
             

            return true;
        }
       
    }

}